home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / tmpf.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  91 lines

  1. /***********************************************************************
  2.  * $Id: tmpf.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *    Temprory files.
  8.  ***********************************************************************/
  9.  
  10. #if !defined(lint) && defined(F_ID)
  11. char *id_tmpf = "$Id: tmpf.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  12. #endif
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include <malloc.h>
  19. #include "ulib.h"
  20.  
  21. #if defined(__STDC__) || defined(__STRICT_ANSI__)
  22. extern char *tempnam(const char *, const char *);
  23. #endif
  24.  
  25. #define MAXTMPF  50
  26. static char *fname[MAXTMPF];
  27.  
  28. /******* search the table for a name ******/
  29. static char **
  30. find_slot(register const char *s)
  31. {
  32.     register char **p = fname, **ps;
  33.  
  34.     for (ps = p + MAXTMPF; p < ps; p++)
  35.     if (*p == s)
  36.         return p;
  37.     fputs("tmpf: can't find avialable space\n", stderr);
  38.     /* overwrite the first one if is a request */
  39.     return s ? 0 : fname;
  40. }
  41.  
  42. /********** get a tmp file name **************/
  43. char *
  44. get_tmpf(const char *pref)
  45. {
  46.     char *p;
  47.  
  48.     /* It appears that SGI's tempnam has a bug that tempnam sets the errno */
  49.     int oerrno = errno;
  50.     /* find slot always returns a valid one */
  51.     p = *(find_slot(0)) = tempnam(0, pref);
  52.     errno = oerrno;
  53.     return p;
  54. }
  55.  
  56. /******* delete the tmp file ******************/
  57. void
  58. del_tmpf(char *name)
  59. {
  60.     char **p;
  61.  
  62.     if (!name || !(p = find_slot(name)))
  63.       {
  64.       fputs("del_tmpf: bad argument\n", stderr);
  65.       return;
  66.       }
  67.  
  68.     if (access(name, F_OK) == 0 && remove(name) == 0)
  69.       {
  70.       free(name);
  71.       *p = 0;
  72.       }
  73. }
  74.  
  75. /******** remove all tmp files used. **********/
  76. void
  77. del_all_tmpf(void)
  78. {
  79.     char **p = fname, **ps;
  80.  
  81.     for (ps = p + MAXTMPF; p < ps; p++)
  82.       {
  83.       if (*p)
  84.         {
  85.         remove(*p);
  86.         free(*p);
  87.         *p = 0;
  88.         }
  89.       }
  90. }
  91.